using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using JigLibX.Physics;
using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Math;
using JigLibX.Utils;

namespace Game1
{
    public class InGameScreen : Screen
    {
        Camera currentCamera = null;
        public Network trader = null;
        //Player player;
        PhysicsSystem physicSystem;
        //Animated animatedgirl;
        BoxObject fallingBox;
        BoxObject immovableBox;
        Model box;
        Player player;

        public InGameScreen(Network trader)
        {
            this.trader = trader;
        }

        protected override void LoadContent()
        {
            AudioManager.playOneMusic("batalha");
            physicSystem = new PhysicsSystem();
            //physicSystem.CollisionSystem = new CollisionSystemGrid(32, 32, 32, 30, 30, 30);
            physicSystem.CollisionSystem = new CollisionSystemSAP();

            // physicSystem.EnableFreezing = true;
            //physicSystem.SolverType = PhysicsSystem.Solver.Normal;
            //physicSystem.CollisionSystem.UseSweepTests = true;

            //physicSystem.NumCollisionIterations = 5;
            //physicSystem.NumContactIterations = 15;
            //physicSystem.NumPenetrationRelaxtionTimesteps = 20;

            physicSystem.Gravity = Vector3.Zero;    
            GuiManager.Singleton.removeAllEntries();
            GuiManager.Singleton.showCursor = false;
            
            //currentCamera = Entities.createCamera("PlayerCam") as Camera;
            currentCamera = new Camera("playercam");
            currentCamera.setPosition(new Vector3(0,10,20));
            //MovableObject.createPlayer("MainPlayer", "p1_wedge");
            box = Base.Content.Load<Model>("Models\\debris");
            
            graphics.GraphicsDevice.Reset();
            Random rand = new Random();
            for (int i = 0; i < 10; i++)
            {
                float a = (float)rand.NextDouble();
                float b = (float)rand.NextDouble();
                float c = (float)rand.NextDouble();
                
                fallingBox = new BoxObject(box, new Vector3(15*a, 15*b, 15*c), Matrix.Identity, new Vector3(-15 + 30 * (float)rand.NextDouble(), 1 + 0.5f * i, -15 + 30 * (float)rand.NextDouble()), currentCamera);
                fallingBox.Scale = new Vector3(15*a,15*b,15*c);
                //immovableBox = new BoxObject("box",new Vector3(0, -5, 0), new Vector3(5, 5, 5), currentCamera);
                //            immovableBox.Body.Immovable = true;
                

                fallingBox.PhysicsBody.Velocity = new Vector3(-30+60 * (float)rand.NextDouble(),
                                                              -30+60 * (float)rand.NextDouble(),
                                                              -30+60 * (float)rand.NextDouble());
                Base.Singleton.Components.Add(fallingBox);
                PhysicEntitities.Add(fallingBox);
            }

            PhysicObject nave = new BoxObject(Base.Content.Load<Model>("Models\\p1_wedge"),
                new Vector3(2, 1, 2), Matrix.Identity, new Vector3(0, 5, 5), currentCamera);
            //immovableBox = new BoxObject("box",new Vector3(0, -5, 0), new Vector3(5, 5, 5), currentCamera);
            //            immovableBox.Body.Immovable = true;
            nave.Scale = new Vector3(0.001f, 0.001f, 0.001f);
            Base.Singleton.Components.Add(nave);

            Model level = Base.Content.Load<Model>("Models\\cenario1");
           // PhysicObject planeObj = new PlaneObject(plane, 0,currentCamera);
            PhysicObject scenery = new TriangleMeshObject(level, Matrix.Identity, Vector3.Zero, currentCamera);
            scenery.PhysicsBody.Immovable = true;
                        
            Base.Singleton.Components.Add(scenery);
            //PhysicEntitities.Add(planeObj);

            player = new Player("joao da silva", nave);
            Entitities.player = player;  

        }

        protected void unLoadContent()
        {
            //Entities.RemoveAll();
            Base.Singleton.Components.Clear();
            Base.currentScreen = new MenuScreen();
        }

        public override void Update(GameTime gameTime)
        {
            //Entities.UpdateAll(gameTime);

            if (Input.IsKeyPressed(Keys.Escape) )
	        {
                unLoadContent();
	        }
            if (Input.IsKeyPressed(Keys.F11) )
            {
                Base.getGraphicsDevice().ToggleFullScreen();
            }
            player.Update(gameTime);
            PhysicsSystem.CurrentPhysicsSystem.Integrate((float) gameTime.ElapsedGameTime.TotalSeconds);

            if (trader != null)
            {
                trader.Update();
            }
        }
      
        public override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            //Entities.drawAll(gameTime,currentCamera);
            
        }
    }
}
